package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.TitleAMount;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.vo.AnalysisByDayVo;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class AnalysisManager {

    @DubboReference
    private AnalysisByDayService analysisByDayService;

    public ResponseEntity summary() {
        AnalysisSummaryVo vo = analysisByDayService.findAnalysisSummaryVo();
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity users(String startDate, String endDate, Integer type) throws ParseException {
        //转换日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long startDatel = Long.parseLong(startDate);
        Long endDatel = Long.parseLong(endDate);

        startDate = simpleDateFormat.format(new Date(Long.parseLong(startDate)));//今年的开始日期
        endDate = simpleDateFormat.format(new Date(Long.parseLong(endDate)));//今年的结束日期
        // 计算去年日期
        Integer lastStartYear = Integer.parseInt(startDate.substring(0, 4)) - 1;//去年开始年份
        Integer lastEndYear = Integer.parseInt(endDate.substring(0, 4)) - 1;//去年结束年份
        String lastStarDate = startDate
                .replace(startDate.substring(0, 4), lastStartYear + "");//去年的开始日期
        String lastEndDate = endDate
                .replace(endDate.substring(0, 4), lastEndYear + "");//去年的结束日期

        //调用rpc查询
        List<AnalysisByDay> analysAllDay = analysisByDayService.findAnalysAllDay(startDate, endDate);
        List<AnalysisByDay> lastanalysAllDay = analysisByDayService.findAnalysAllDay(lastStarDate, lastEndDate);

        //声明返回结果对象
        List<TitleAMount> nowYear = new ArrayList<>();
        List<TitleAMount> LastYear = new ArrayList<>();
        Long size = (endDatel - startDatel) / (24 * 3600 * 1000L) + 1;
        //封装日期对象
        for (long i = 0; i <size ; i++) {
            TitleAMount titleAMount = new TitleAMount();
            titleAMount.setTitle(startDate);
            titleAMount.setAmount(0);
            startDatel = startDatel + 24 * 3600 * 1000;
            startDate = simpleDateFormat.format(new Date(startDatel));
            nowYear.add(titleAMount);
        }
        //封装结束
        long lasttime = simpleDateFormat.parse(lastEndDate).getTime();//去年结束时间的毫秒值
        long starttime = simpleDateFormat.parse(lastStarDate).getTime();//去年开始时间的毫秒值
        long num =  (lasttime - starttime) / (24 * 3600 * 1000) + 1;
        for (long i = 0; i < num; i++) {
            TitleAMount titleAMount = new TitleAMount();
            titleAMount.setTitle(lastStarDate);
            titleAMount.setAmount(0);
            starttime = starttime + 24 * 3600 * 1000;
            lastStarDate = simpleDateFormat.format(new Date(starttime));
            LastYear.add(titleAMount);
        }


        //封装vo
        AnalysisByDayVo vo = new AnalysisByDayVo();

        //今年的数据
        if (type == 101) {
            //今年的数据
            if (CollectionUtil.isNotEmpty(analysAllDay)) {
                for (AnalysisByDay analysisByDay : analysAllDay) {
                    for (TitleAMount titleAMount : nowYear) {
                        //如果日期相同
                        if (simpleDateFormat.format(analysisByDay.getRecordDate()).equals(titleAMount.getTitle())) {
                            //封装数量
                            titleAMount.setAmount(analysisByDay.getNumRegistered());
                        }
                    }

                }
            }
            //去年的数据
            if (CollectionUtil.isNotEmpty(lastanalysAllDay)) {
                for (AnalysisByDay analysisByDay : lastanalysAllDay) {
                    for (TitleAMount titleAMount : LastYear) {
                        //如果日期相同
                        if (simpleDateFormat.format(analysisByDay.getRecordDate()).equals(titleAMount.getTitle())) {
                            //封装数量
                            titleAMount.setAmount(analysisByDay.getNumRegistered());
                        }
                    }
                }
            }
        } else if (type == 102) {
            //今年的数据
            if (CollectionUtil.isNotEmpty(analysAllDay)) {
                for (AnalysisByDay analysisByDay : analysAllDay) {
                    for (TitleAMount titleAMount : nowYear) {
                        //如果日期相同
                        if (simpleDateFormat.format(analysisByDay.getRecordDate()).equals(titleAMount.getTitle())) {
                            //封装数量
                            titleAMount.setAmount(analysisByDay.getNumActive());
                        }
                    }

                }
            }
            //去年的数据
            if (CollectionUtil.isNotEmpty(lastanalysAllDay)) {
                for (AnalysisByDay analysisByDay : lastanalysAllDay) {
                    for (TitleAMount titleAMount : LastYear) {
                        //如果日期相同
                        if (simpleDateFormat.format(analysisByDay.getRecordDate()).equals(titleAMount.getTitle())) {
                            //封装数量
                            titleAMount.setAmount(analysisByDay.getNumActive());
                        }
                    }

                }
            }

        } else if (type == 103) {
            //今年的数据
            if (CollectionUtil.isNotEmpty(analysAllDay)) {
                for (AnalysisByDay analysisByDay : analysAllDay) {
                    for (TitleAMount titleAMount : nowYear) {
                        //如果日期相同
                        if (simpleDateFormat.format(analysisByDay.getRecordDate()).equals(titleAMount.getTitle())) {
                            //封装数量
                            titleAMount.setAmount(analysisByDay.getNumRetention1d());
                        }
                    }

                }
            }
            //去年的数据
            if (CollectionUtil.isNotEmpty(lastanalysAllDay)) {
                for (AnalysisByDay analysisByDay : lastanalysAllDay) {
                    for (TitleAMount titleAMount : LastYear) {
                        //如果日期相同
                        if (simpleDateFormat.format(analysisByDay.getRecordDate()).equals(titleAMount.getTitle())) {
                            //封装数量
                            titleAMount.setAmount(analysisByDay.getNumRetention1d());
                        }
                    }
                }
            }

        }
        vo.setThisYear(nowYear);
        vo.setLastYear(LastYear);
        return ResponseEntity.ok(vo);
    }
}
